package com.harmonyos.samples.view;

import com.harmonyos.samples.bean.repository.RepositoriesInfo;
import com.harmonyos.samples.constants.Constants;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBLabel;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.IOException;

public class WaterFlowItem extends JPanel {
    private final RepositoriesInfo data;
    private boolean selected;
    private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

    private String selectCategory;

    public WaterFlowItem(RepositoriesInfo data, String selectCategory) {
        this.data = data;
        this.selectCategory = selectCategory;
        setLayout(new BorderLayout());
        if (selectCategory.equals(Constants.PLUGIN_CATEGORY_SAMPLE)) {
            JLabel imageLabel = new JLabel();
            imageLabel.setVerticalAlignment(SwingConstants.CENTER);
            imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
            if (data.getLocalPath() != null && !data.getLocalPath().isEmpty()) {
                try {
                    File file = new File(data.getLocalPath());
                    if (file.isFile() && file.exists()) {
                        BufferedImage originalImage = ImageIO.read(file);
                        if (originalImage != null) {
                            int scaleWidth = Constants.WATER_FLOW_ITEM_WIDTH - Constants.WATER_FLOW_ITEM_PADDING_LEFT - Constants.WATER_FLOW_ITEM_PADDING_RIGHT;
                            double widthRatio = (double) scaleWidth/ originalImage.getWidth();
                            int scaleHeight = (int) (originalImage.getHeight() * widthRatio);
                            setPreferredSize(new Dimension(Constants.WATER_FLOW_ITEM_WIDTH, Math.max(scaleHeight + Constants.WATER_FLOW_ITEM_PADDING_TOP + Constants.WATER_FLOW_ITEM_PADDING_BOTTOM + Constants.WATER_FLOW_ITEM_TEXT_HEIGHT, Constants.WATER_FLOW_ITEM_IMAGE_MIN_HEIGHT)));

                            Image image = data.getIcon().getImage().getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_DEFAULT);
                            imageLabel.setIcon(new ImageIcon(image));
                        } else {
                            file.delete();
                        }
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            add(imageLabel, BorderLayout.CENTER);
            JBLabel textLabel = new JBLabel(data.getName(), SwingConstants.CENTER);
            textLabel.setPreferredSize(new Dimension(Constants.WATER_FLOW_ITEM_WIDTH - (Constants.WATER_FLOW_ITEM_PADDING_LEFT + Constants.WATER_FLOW_ITEM_PADDING_RIGHT), Constants.WATER_FLOW_ITEM_TEXT_HEIGHT));
            add(textLabel, BorderLayout.SOUTH);
        }
        if (selectCategory.equals(Constants.PLUGIN_CATEGORY_LIBRARY)) {
            JBLabel textLabel = new JBLabel(data.getName(), SwingConstants.LEFT);
            textLabel.setFont(textLabel.getFont().deriveFont(18f));
            textLabel.setForeground(JBColor.BLUE);
            add(textLabel, BorderLayout.NORTH);
            JBLabel descLabel = new JBLabel("<html>" + data.getDescription() + "</html>", SwingConstants.LEFT);
            add(descLabel, BorderLayout.CENTER);
        }
        setBackground(null);
        setBorder(BorderFactory.createEmptyBorder(Constants.WATER_FLOW_ITEM_PADDING_TOP, Constants.WATER_FLOW_ITEM_PADDING_LEFT, Constants.WATER_FLOW_ITEM_PADDING_BOTTOM, Constants.WATER_FLOW_ITEM_PADDING_RIGHT));
        setOpaque(false);

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (selected) {
            g.setColor(JBColor.BLUE);
            g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        } else {
            g.setColor(JBColor.PanelBackground);
            g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        }
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
        repaint();
    }

}
